package com.software.levelupandroid.notion.activity;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.Resource;
import com.software.levelupandroid.entity.SystemTask;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.home.ui.home.HomeViewModel;
import com.software.levelupandroid.notion.adapter.ItemAdapter;
import com.software.levelupandroid.systemTask.model.SystemShowViewModel;
import com.software.levelupandroid.utils.TitleBar;
import com.software.levelupandroid.utils.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NotionActivity extends AppCompatActivity {
    TitleBar titleBar;
    private Toolbar toolbar;
    private HomeViewModel homeViewModel;
    LinearLayout nightLayout, dayLayout;

    private TextView nightTime, dayTime;

    private ListView nightListView, overListView,systemListView, dayListView;
    private TextView nightCount, overCount, dayCount,systemCount;


    private SystemShowViewModel systemShowViewModel;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notion);

        titleBar = new TitleBar(findViewById(R.id.notion_title_bar));

        findViews();

        initViewModel();

        initData();
        /**
         * 此刻时间与晚上8点比较，8点之后，显示夜晚任务
         * 8点之前，只显示白天任务*/


        if (Integer.parseInt(new SimpleDateFormat("HH").format(new Date())) >=20) {
            nightLayout.setVisibility(LinearLayout.VISIBLE);
            dayLayout.setVisibility(LinearLayout.VISIBLE);
            //获取当前时间
            String date = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
            nightTime.setText(date);
            String date1 = new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
            dayTime.setText(date1 + " 08:00");
        } else if (Integer.parseInt(new SimpleDateFormat("HH").format(new Date())) < 8){
            nightLayout.setVisibility(LinearLayout.GONE);
            dayLayout.setVisibility(LinearLayout.VISIBLE);
            //获取当前时间
            String date = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
            dayTime.setText(date);

        } else {
            String date1 = new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
            dayTime.setText(date1 + " 08:00:00");
            nightLayout.setVisibility(LinearLayout.GONE);
            dayLayout.setVisibility(LinearLayout.VISIBLE);
        }

        //todo 只要拿到了TitleBar 的对象
        titleBar.showLeftImageView(true);
        titleBar.setTitleText("通知中心");



    }

    private void findViews() {
        toolbar = findViewById(R.id.toolbar_notion);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();

        if (actionBar != null)
            actionBar.setDisplayShowTitleEnabled(false);
        nightLayout = findViewById(R.id.night_title_layout);
        systemCount = findViewById(R.id.clock_content);
        nightListView = findViewById(R.id.night_list);
        nightTime = findViewById(R.id.night_time);
        nightCount = findViewById(R.id.todo);
        overListView = findViewById(R.id.over_list);
        dayListView = findViewById(R.id.tody_list);
        systemListView = findViewById(R.id.clock_list);
        dayTime = findViewById(R.id.time);
        overCount = findViewById(R.id.over_content);
        dayCount = findViewById(R.id.tody_content);
        dayLayout = findViewById(R.id.day_layout);

    }

    private void initData() {
        //todo 从数据库中拿最近三日数据

        homeViewModel.getAllTasksByDate(this);
        observeLiveData();

        systemShowViewModel.getAllSystemTask(this);
        observeSystemLiveData();
        homeViewModel.getTomorrowTasksByDate(this);
        observeTomorrowLiveData();





    }

    private void observeTomorrowLiveData() {
        homeViewModel.getTaskListLiveData().observe(this, new Observer<Resource<List<Task>>>() {

            @Override
            public void onChanged(Resource<List<Task>> listResource) {
                if (listResource != null){
                    if (listResource.isSuccess()){
                        List<String> tomorrowTasks = new ArrayList<>();
                        List<Task> taskList = listResource.getData();
                        for (int i = 0; i < taskList.size(); i++) {
                           tomorrowTasks.add(taskList.get(i).getTaskTitle());
                        }

//                        ItemAdapter adapter2 = new ItemAdapter(NotionActivity.this, tomorrowTasks);
//                        nightListView.setAdapter(adapter2);


                    }else if (listResource.isFailure()){
                        ToastUtil.showToastOnUI(NotionActivity.this, listResource.getErrorMessage());
                    } else if (listResource.isLoading()) {

                    }
                }

            }
        });

    }

    private void initViewModel() {
        ViewModelProvider viewModelProvider = new ViewModelProvider(this);
        homeViewModel = viewModelProvider.get(HomeViewModel.class);
        systemShowViewModel = viewModelProvider.get(SystemShowViewModel.class);
    }

    @Override
    public void onResume() {
        super.onResume();
    }





    private void observeSystemLiveData() {
        systemShowViewModel.getSystemTaskListLiveData().observe(this, new Observer<Resource<List<SystemTask>>>() {
            @Override
            public void onChanged(Resource<List<SystemTask>> listResource) {
                List<String> taskList = new ArrayList<>();

                if (listResource != null){
                    if (listResource.isSuccess()){
                        List<SystemTask> systemData = listResource.getData();
                        for (int i = 0; i < systemData.size(); i++) {
                            taskList.add(systemData.get(i).getTitle());

                        }

                        final boolean[] isToday = {false};
                        systemCount.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                isToday[0] = !isToday[0];
                            }
                        });
                        ItemAdapter adapter = new ItemAdapter(NotionActivity.this, taskList, isToday[0]);
                        systemListView.setAdapter(adapter);


                    }else if (listResource.isFailure()){
                        ToastUtil.showToastOnUI(NotionActivity.this, listResource.getErrorMessage());
                    } else if (listResource.isLoading()) {

                    }
                }
            }
        });


    }
    private void observeLiveData() {
        homeViewModel.getTaskListLiveData().observe(this, new Observer<Resource<List<Task>>>() {

            @Override
            public void onChanged(Resource<List<Task>> listResource) {
                if (listResource != null){
                    if (listResource.isSuccess()){
                        List<String> dayTasks = new ArrayList<>();
                        List<String> overTasks = new ArrayList<>();
                        List<String> tomorrowTasks = new ArrayList<>();
                        List<Task> taskList = listResource.getData();
                        for (int i = 0; i < taskList.size(); i++) {
                            if (taskList.get(i).getStatus() == 0){
                                dayTasks.add(taskList.get(i).getTaskTitle());
                            }else if (taskList.get(i).getStatus() == -1){
                                overTasks.add(taskList.get(i).getTaskTitle());
                            }
                        }
                        //要做的任务
//                        ItemAdapter adapter = new ItemAdapter(NotionActivity.this, dayTasks);
//                        dayListView.setAdapter(adapter);
                        dayCount.setText("今日待办：" + dayTasks.size() + "件");
                        //要做任务的健壮性
                        if(dayTasks == null){
                            dayCount.setText("今日待办：0件");
                        }
                        //已完成的任务
//                        ItemAdapter adapter1 = new ItemAdapter(NotionActivity.this, overTasks);
//                        overListView.setAdapter(adapter1);
                        overCount.setText("已完成：" + overTasks.size() + "件");
                        if(overTasks == null){
                            overCount.setText("已完成：0件");
                        }


//                        ItemAdapter adapter2 = new ItemAdapter(NotionActivity.this, tomorrowTasks);
//                        nightListView.setAdapter(adapter2);


                    }else if (listResource.isFailure()){
                        ToastUtil.showToastOnUI(NotionActivity.this, listResource.getErrorMessage());
                    } else if (listResource.isLoading()) {

                    }
                }

            }
        });


    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }
}